﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using BusinessLayer.Model.Services;
using BusinessLayer;
using System.IO;
using BusinessLayer.Model.Repositories.Abstract;
using BusinessLayer.Model.Repositories;
using BusinessLayer.Model.Entities;

using BusinessLayer.Model.Services.Abstract;
using BusinessLayer.Model.Services.Concrete;
using DataAccess.Services.Abstract;

namespace intecnologia
{
    public class Global : System.Web.HttpApplication
    {
        IApplicationService appService = new ApplicationService();

        protected void Application_Start(object sender, EventArgs e)
        {
            //chequear que el path lo devuelve con '\\' al final,
            //localmente funciona pero estaba dando error en el IIS en intec.

            string mapPath = Server.MapPath("~");

            appService.ApplicationFolder = (mapPath.Substring(mapPath.Length - 1) == "\\") ?
                                                mapPath : mapPath + "\\";
            
            appService.DataFolder = appService.ApplicationFolder + Constants.DataFolder + "\\";
            appService.XmlFilesFolder = appService.DataFolder + Constants.XmlFilesFolder + "\\";
            appService.LogsFolder = appService.DataFolder + Constants.LogsFolder + "\\";
            appService.NewsFilesFolder = appService.DataFolder + Constants.NewsFolder + "\\";

            appService.GalleryFolder = appService.ApplicationFolder + Constants.GalleryImagesFolder + "\\";
            appService.BannerFolder = appService.ApplicationFolder + Constants.BannersFolderName + "\\";

            appService.GenerateSystemFoldersIfInvalid();

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (appService.ApplicationURL == null)
            {
                appService.ApplicationURL = Request.Url.Scheme +
                                            "://" + Request.Url.Authority +
                                            Request.ApplicationPath;

                appService.ApplicationURL = (appService.ApplicationURL.Substring(appService.ApplicationURL.Length - 1) == "/") ? appService.ApplicationURL : appService.ApplicationURL + "/";
                                                                
                     
            }
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            //IExceptionReportService exceptionReportService = new ExceptionReportService();
            //try
            //{
            //    Exception exc = Server.GetLastError();
            //    string url = Request.Url.ToString();

            //    //forze para favicon.ico (un bug que tiene el editor de html que causa que
            //    //                        se llene el log de errores)

            //    if (url.Contains("favicon.ico") || url.Contains("rp_blt.gif"))
            //        return;

            //    Application.Lock();
            //    Application["ExceptionRequestUrl"] = Request.Url.ToString();
            //    Application.UnLock();

            //    exceptionReportService.LogExceptionInHtml(exc, url);
            //    exceptionReportService.LogException(exc, exc.Source, url);
            //}
            //catch (Exception)
            //{
            //}
        }

        protected void Session_End(object sender, EventArgs e)
        {
            new SessionService().ClearUserSession();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}